$(function () {
    cartMath();
    /*当浏览器为IE6、7时，弹出升级浏览器提示框*/
    //judgeIE();
});

function cartMath() {
    var x = Number($(".maintop .cart .a1 span").text());
    if (x > 99) {
        $(".maintop .cart .a1 span").addClass("small");
    }
}


/*一个页面多个TAB切换*/
function nTabs(thisObj, Num) {
    if (thisObj.className == "active") return;
    var tabObj = thisObj.parentNode.id;
    var tabList = document.getElementById(tabObj).getElementsByTagName("li");
    for (var i = 1; i < tabList.length+1; i++) {
        if (i == Num) {
            thisObj.className = "active";
            document.getElementById(tabObj + "_Content_" + i).style.display = "block";
        } else {
            tabList[i-1].className = "normal";
            document.getElementById(tabObj + "_Content_" + i).style.display = "none";
        }
    }
}
var configs = {
    s_url:$.trim($(".shop_site_path").val()),
    api_key:$.trim($(".shop_api_key").val()),
    api_sgin:$.trim($(".shop_api_sgin").val()),
    user_id:$.trim($(".shop_user_id").val())
}

var searchHttpHelper={
    s_url: configs.s_url+"pcorder/index/searchTips",
    params:'params={"api_key":"'+configs.api_key+'","user_id":"'+configs.user_id+'","api_sgin":"'+configs.api_sgin+'","client_type":2,"ver":13,',
    getHots:function(currentObj,fileName,hiddenUrl){
        var params = searchHttpHelper.params+'"m":"t_26_sousuolishi","service":"Grain.searchHistory","task_id":"hots"}';
        this.doPost(params,currentObj,fileName,hiddenUrl);
    },
    deleteHots:function(currentObj){
        var params = searchHttpHelper.params+'"m":"t_26_sousuolishi","service":"Grain.deleteHistory","task_id":"delhots"}';
        this.doPost(params,currentObj);
    },
    getMenuList:function(currentObj,fileName,hiddenUrl,kindtype,keyword,hidden_img){
        //kindtype=1 实时搜索
        if(kindtype==1){
            var params = searchHttpHelper.params+'"keyword":"'+keyword+'","service":"Grain.searchTips","task_id":"menulist"}';
        }else{
            var params = searchHttpHelper.params+'"m":"t_26_menu","ord":["order_id asc"],"map":[["m_name","like","'+keyword+'"]],"product_size":"","service":"Grain.select","page_size":20,"task_id":"menulist"}';
        }
        $(".cart-panel-ul").children('*').remove();
        this.doPost(params,currentObj,fileName,hiddenUrl,kindtype,hidden_img);
    },
    doPost:function(params,currentObj,fileName,hiddenUrl,kindtype,hidden_img){
        var result = {};
        $.ajax({
            type:"post",
            url:this.s_url,
            dataType:'json',
            data:params,
            success:function Response(data){
                searchHttpHelper.callback(data,currentObj,fileName,hiddenUrl,kindtype,hidden_img);
            }
        });
        return result;
    },
    callback:function(obj,currentObj,fileName,hiddenUrl,kindtype,hidden_img){
        switch (obj.task_id){
            case 'menulist':
                if(obj.code==200) {
                    $(".cart-panel-ul").html('');
                    currentObj.show();
                    if(kindtype==1){
                        var html='';
                        if(obj.list.length>0){
                            for( var i=0;i<obj.list.length;i++) {
                                html += '<li class="menu"><a href="'+hiddenUrl+obj.list[i].m_id+'">' + obj.list[i].m_name + '</a></li>';
                            }
                        }
                    }else{
                        var html='';
                        if(obj.list.length>0){
                            for( var i=0;i<obj.list.length;i++){
                                var spec='';
                                if(obj.list[i].m_spec==0){
                                    spec='斤';
                                }else if(obj.list[i].m_spec==1) {
                                    spec='包';
                                }else if(obj.list[i].m_spec==2){
                                    spec='袋';
                                }else if(obj.list[i].m_spec==3){
                                    spec='个';
                                }else if(obj.list[i].m_spec==4){
                                    spec='条';
                                }else if(obj.list[i].m_spec==5){
                                    spec='件';
                                }else if(obj.list[i].m_spec==6){
                                    spec='箱';
                                }else if(obj.list[i].m_spec==7){
                                    spec='瓶';
                                }else if(obj.list[i].m_spec==11){
                                    spec='大号';
                                }else if(obj.list[i].m_spec==12){
                                    spec='小号';
                                }else if(obj.list[i].m_spec==13){
                                    spec='盒';
                                }else{
                                    spec=obj.list[i].tag_name;
                                }
                                html+='<li class="menu"><a href="'+hiddenUrl+obj.list[i].item_no+'"><div class="clearfix h-order"><div class="imgpanel">';
                                if(obj.list[i].attach[0]){
                                    var image=obj.list[i].attach[0]['file_name'];
                                    html+='<img src="'+fileName+image+'" class="am-img-responsive"/>';
                                }else{
                                    html+='<img src="'+hidden_img+'" class="am-img-responsive"/>';
                                }
                                html+='</div><div class="infopanel " style="margin-top:15px;">';
                                html+='<h3>'+obj.list[i].m_name+'</h3>';
                                html+='<p> <span class="red2">'+obj.list[i].m_price+'元</span>/'+spec+'</p>';
                                html+=' </div> </div> </a></li>';
                            }
                        }else{
                            html+='<li style="text-align:center">您搜索的商品不存在</li>';
                        }

                    }					
                    currentObj.append(html);
                    $("input.hvalue").blur(function(){
                        $(".cart-panel-ul").hide();

                    })
                }else{
                    //alert('error');
                }
                break;
            case 'hots':
                if(obj.code==200) {
                    $(".cart-panel-ul").html('');
                    var html='';
                    html+=' <li class="hotso" style="border-bottom: 1px solid #ccc ;">';

                  if(obj.hots.length>0){
                      html+='<div style="margin-left: 10px; color:#19b1e3"><span>热搜:</span><div>'
                              for(var i=0;i<obj.hots.length;i++){
                                  html+='<a href="'+hiddenUrl+obj.hots[i].keyword+'">'+obj.hots[i].keyword+'</a>'
                              }
                      html+='</div>'; html+='</div>';
                  }
                    if(obj.history.length>0){

                        html+=' <div style="margin: 10px;"><h3>历史搜索</h3>'
                        html+='<div class="history"> '
                        for(var i=0;i<obj.history.length;i++){
                            html+='<a href="'+hiddenUrl+obj.history[i].keyword+'">'+obj.history[i].keyword+'</a>'
                        }
                        html+='</div>';
                        html+='<div class="hdelete">清空历史搜索</div>';
                        html+='</div>';
                    }
                    html+=' </li>';
                    currentObj.append(html);
                    $(".hdelete").click(function(){
                        searchHttpHelper.deleteHots($(".history"));
                    })
                }
                break;
            case 'delhots':
                if(obj.code==200) {
                    currentObj.html('');
                }
                break;

            default :
                //alert('error');
                break;
        }


    }
}



